home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / prgmming / swag05 / comm.swg < prev    next >
Encoding:
Text File  |  1994-09-22  |  11.9 KB  |  1 lines

  1. SWAGOLX.EXE (c) 1993 GDSOFT  ALL RIGHTS RESERVED 00003                                                                           1      05-25-9408:14ALL                      GREG VIGNEAULT           Reading UART baud rate...SWAG9405            22     ╣J   π{π Here's a TP function that will report the current UART baud rate forπ any serial port device (modem, mouse, etc.) ...π}ππ(*************************** GETBAUD.PAS ***************************)πPROGRAM GetBaud;                      { compiler: Turbo Pascal 4.0+ }π                                      { Mar.23.94 Greg Vigneault    }ππ(*-----------------------------------------------------------------*)π{ get the current baud rate of a serial i/o port (reads the UART)...}ππFUNCTION SioRate (ComPort :WORD; VAR Baud :LONGINT) :BOOLEAN;π  CONST DLAB = $80;                   { divisor latch access bit    }π  VAR   BaseIO,                       { COM base i/o port address   }π        BRGdiv,                       { baud rate generator divisor }π        regDLL,                       { BRG divisor, latched LSB    }π        regDLM,                       { BRG divisor, latched MSB    }π        regLCR :WORD;                 { line control register       }π  BEGINπ    Baud := 0;                                { assume nothing      }π    IF (ComPort IN [1..4]) THEN BEGIN         { must be 1..4        }π      BaseIO := MemW[$40:(ComPort-1) SHL 1];  { fetch base i/o port }π      IF (BaseIO <> 0) THEN BEGIN             { has BIOS seen it?   }π        regDLL := BaseIO;                     { BRGdiv, latched LSB }π        regDLM := BaseIO + 1;                 { BRGdiv, latched MSB }π        regLCR := BaseIO + 3;                 { line control reg    }π        Port[regLCR] := Port[regLCR] OR DLAB;         { set DLAB    }π        BRGdiv := WORD(Port[regDLL]);                 { BRGdiv LSB  }π        BRGdiv := BRGdiv OR WORD(Port[regDLM]) SHL 8; { BRGdiv MSB  }π        Port[regLCR] := Port[regLCR] AND NOT DLAB;    { reset DLAB  }π        IF (BRGdiv <> 0) THENπ          Baud := 1843200 DIV (LONGINT(BRGdiv) SHL 4);  { calc bps  }π      END; {IF BaseIO}π    END; {IF ComPort}π    SioRate := (Baud <> 0);                   { success || failure  }π  END {SioRate};ππ(*-----------------------------------------------------------------*)ππVAR ComPort : WORD;                         { will be 1..4          }π    Baud    : LONGINT;                      { as high as 115200 bps }ππBEGIN {GetBaud}ππ  REPEATπ    WriteLn; Write ('Read baud rate for which COM port [1..4] ?: ');π    ReadLn (ComPort);π    IF NOT SioRate (ComPort, Baud) THEN BEGINπ      Write ('!',CHR(7)); {!beep}π      CASE ComPort OFπ        1..4 : WriteLn ('COM',ComPort,' is absent; try another...');π        ELSE WriteLn ('Choose a number: 1 through 4...');π      END; {CASE}π    END; {IF}π  UNTIL (Baud <> 0);ππ  WriteLn ('-> COM',ComPort,' is set for ',Baud,' bits-per-second');ππEND {GetBaud}.π                     2      05-25-9408:24ALL                      JONAS MALMSTEN           UART                     SWAG9405            32     ╣J   π{πI've read some questions latelly with questions about how to use a com-port inπpascal. I've written a couple of procedures for doing this. The followingπroutines can be improved, for example they can be satt to run on interruptsπand a few other thing, but... I'm not supposed to do all the job for you, amπI??π}ππUSES CRT,DOS;πππCONSTπ     Com1 : WORD = 1;π     Com2 : WORD = 2;ππtypeπ    port = objectπ       port: byte;π       base: word;π       baud: longint;π       inter: byte;π       function init(comport: word; baudrate: longint): boolean;π       function sendchar(c: char): boolean;π       function getchar(var c: char): boolean;π    end;ππfunction port.init(comport: word; baudrate: longint): boolean;πvarπ   tmp: word;π   bas: word;π   test: byte;πbeginπ     if odd(comport) then inter:=$C else inter:=$B;π                          {This is for later use with interrupts...}π     init:=false;π     if comport<5 thenπ     beginπ          asm {get port base address}π             mov bx,40hπ             mov es,bxπ             mov bx,comportπ             dec bxπ             shl bx,1π             mov ax,es:[bx]π             mov bas,axπ          end;π          if bas=0 thenπ          beginπ               writeln('Could''n find selected com-port!');π               exit;π          end;π     endπ     elseπ     beginπ          case comport of {don't know where to find ps/2 etdπ                           bios, standard base is supposed}π            5: bas:=$4220;π            6: bas:=$4228;π            7: bas:=$5220;π            8: bas:=$5228;π          end;π     end;π     base:=bas;π     tmp:=115200 div baudrate; {baudrate divisor}π     asm {lower DTS and DSR}π        mov dx,basπ        add dx,4π        xor al,alπ        out dx,alπ     end;π     delay(50);π     asm {raise DTS and DSR}π        mov dx,basπ        add dx,4π        mov al,11bπ        out dx,alπ     end;π     asm {set baudrate and N,8,1}π        mov dx,basπ        add dx,3π        mov al,10000011b {N,8,1, set baudrate divisor}π        out dx,alπ        mov ax,tmp {baudrate divisor}π        mov dx,basπ        out dx,alπ        inc dxπ        mov al,ahπ        out dx,alπ        mov dx,basπ        add dx,3π        mov al,00000011b {N,8,1}π        out dx,alπ     end;π     asm {interrupt enable, no interrupts enabled --> gain time}π        mov dx,basπ        inc dxπ        xor al,alπ        out dx,alπ     end;π     asm {raise DTS and DSR}π        mov dx,basπ        add dx,4π        mov al,11bπ        out dx,alπ        in al,dxπ        and al,11bπ        mov test,alπ     end;π     if test<>3 thenπ     beginπ          writeln('Some error....');π          exit;π     end;π     init:=true;πend;ππfunction port.sendchar(c: char): boolean;πvarπ   bas: word;π   cts: byte;πlabelπ     no_send;πbeginπ     cts:=0;π     bas:=base;π     asmπ        mov dx,basπ        add dx,5π        in al,dxπ        and al,00100000b {test CTS (Clear To Send status)}π        jz no_sendπ        mov cts,1π        mov dx,basπ        mov al,cπ        out dx,alπ     no_send:π     end;π     if cts=0 then sendchar:=false else sendchar:=true;πend;ππfunction port.getchar(var c: char): boolean;πvarπ   bas: word;π   rts: byte;π   c2: char;πlabelπ     no_data;πbeginπ     rts:=0;π     bas:=base;π     asmπ        mov dx,basπ        add dx,5π        in al,dxπ        and al,00000001b {test for data ready}π        jz no_dataπ        mov rts,1π        mov dx,basπ        in al,dxπ     no_data:π        mov c2,alπ     end;π     c:=c2;π     if rts=0 then getchar:=false else getchar:=true;πend;πππvarπ   modem: port;π   s: string;π   a: byte;π   c : Char;ππbeginπ     if not modem.init(com2,38400) thenπ     beginπ          writeln('Couldn''t initialize modem...');π          halt;π     end;π     s:='atz'+#13;π     for a:=1 to length(s) do modem.sendchar(s[a]);ππend.πππIf you think these routines are just great and you decide to use them as theyπare I wouldn't mind if you gave me a credit.π                 3      05-26-9407:29ALL                      SCOTT BAKER              Fossil Driver            SWAG9405            40     ╣J   unit ddfossil;π{$S-,V-,R-}ππinterfaceπuses dos;ππconstπ name='Fossil drivers for TP 4.0';π author='Scott Baker';πtypeπ fossildatatype = recordπ                   strsize: word;π                   majver: byte;π                   minver: byte;π                   ident: pointer;π                   ibufr: word;π                   ifree: word;π                   obufr: word;π                   ofree: word;π                   swidth: byte;π                   sheight: byte;π                   baud: byte;π                  end;πvarπ port_num: integer;π fossildata: fossildatatype;ππprocedure async_send(ch: char);πprocedure async_send_string(s: string);πfunction async_receive(var ch: char): boolean;πfunction async_carrier_drop: boolean;πfunction async_carrier_present: boolean;πfunction async_buffer_check: boolean;πfunction async_init_fossil: boolean;πprocedure async_deinit_fossil;πprocedure async_flush_output;πprocedure async_purge_output;πprocedure async_purge_input;πprocedure async_set_dtr(state: boolean);πprocedure async_watchdog_on;πprocedure async_watchdog_off;πprocedure async_warm_reboot;πprocedure async_cold_reboot;πprocedure async_Set_baud(n: integer);πprocedure async_set_flow(SoftTran,Hard,SoftRecv: boolean);πprocedure Async_Buffer_Status(var Insize,Infree,OutSize,Outfree: word);ππimplementationππprocedure async_send(ch: char);πvarπ regs: registers;πbegin;π regs.al:=ord(ch);π regs.dx:=port_num;π regs.ah:=1;π intr($14,regs);πend;ππprocedure async_send_string(s: string);πvarπ a: integer;πbegin;π for a:=1 to length(s) do async_send(s[a]);πend;ππfunction async_receive(var ch: char): boolean;πvarπ regs: registers;πbegin;π ch:=#0;π regs.ah:=3;π regs.dx:=port_num;π intr($14,regs);π if (regs.ah and 1)=1 then begin;π  regs.ah:=2;π  regs.dx:=port_num;π  intr($14,regs);π  ch:=chr(regs.al);π  async_receive:=true;π end else async_receive:=false;πend;ππfunction async_carrier_drop: boolean;πvarπ regs: registers;πbegin;π regs.ah:=3;π regs.dx:=port_num;π intr($14,regs);π if (regs.al and $80)<>0 then async_carrier_drop:=false else async_carrier_drop:=true;πend;ππfunction async_carrier_present: boolean;πvarπ regs: registers;πbegin;π regs.ah:=3;π regs.dx:=port_num;π intr($14,regs);π if (regs.al and $80)<>0 then async_carrier_present:=true else async_carrier_present:=false;πend;ππfunction async_buffer_check: boolean;πvarπ regs: registers;πbegin;π regs.ah:=3;π regs.dx:=port_num;π intr($14,regs);π if (regs.ah and 1)=1 then async_buffer_check:=true else async_buffer_check:=false;πend;ππfunction async_init_fossil: boolean;πvarπ regs: registers;πbegin;π regs.ah:=4;π regs.bx:=0;π regs.dx:=port_num;π intr($14,regs);π if regs.ax=$1954 then async_init_fossil:=true else async_init_fossil:=false;πend;ππprocedure async_deinit_fossil;πvarπ regs: registers;πbegin;π regs.ah:=5;π regs.dx:=port_num;π intr($14,regs);πend;ππprocedure async_set_dtr(state: boolean);πvarπ regs: registers;πbegin;π regs.ah:=6;π if state then regs.al:=1 else regs.al:=0;π regs.dx:=port_num;π intr($14,regs);πend;ππprocedure async_flush_output;πvarπ regs: registers;πbegin;π regs.ah:=8;π regs.dx:=port_num;π intr($14,regs);πend;ππprocedure async_purge_output;πvarπ regs: registers;πbegin;π regs.ah:=9;π regs.dx:=port_num;π intr($14,regs);πend;ππprocedure async_purge_input;πvarπ regs: registers;πbegin;π regs.ah:=$0a;π regs.dx:=port_num;π intr($14,regs);πend;ππprocedure async_watchdog_on;πvarπ regs: registers;πbegin;π regs.ah:=$14;π regs.al:=01;π regs.dx:=port_num;π intr($14,regs);πend;ππprocedure async_watchdog_off;πvarπ regs: registers;πbegin;π regs.ah:=$14;π regs.al:=00;π regs.dx:=port_num;π intr($14,regs);πend;ππprocedure async_warm_reboot;πvarπ regs: registers;πbegin;π regs.ah:=$17;π regs.al:=01;π intr($14,regs);πend;ππprocedure async_cold_reboot;πvarπ regs: registers;πbegin;π regs.ah:=$17;π regs.al:=00;π intr($14,regs);πend;ππprocedure async_set_baud(n: integer);πvarπ regs: registers;πbegin;π regs.ah:=00;π regs.al:=3;π regs.dx:=port_num;π case n ofπ  300: regs.al:=regs.al or $40;π  1200: regs.al:=regs.al or $80;π  2400: regs.al:=regs.al or $A0;π  4800: regs.al:=regs.al or $C0;π  9600: regs.al:=regs.al or $E0;π  19200: regs.al:=regs.al or $00;π end;π intr($14,regs);πend;ππprocedure async_set_flow(SoftTran,Hard,SoftRecv: boolean);πvarπ regs: registers;πbegin;π regs.ah:=$0F;π regs.al:=00;π if softtran then regs.al:=regs.al or $01;π if Hard then regs.al:=regs.al or $02;π if SoftRecv then regs.al:=regs.al or $08;π regs.al:=regs.al or $F0;π Intr($14,regs);πend;ππprocedure async_get_fossil_data;πvarπ regs: registers;πbegin;π regs.ah:=$1B;π regs.cx:=sizeof(fossildata);π regs.dx:=port_num;π regs.es:=seg(fossildata);π regs.di:=ofs(fossildata);π intr($14,regs);πend;ππprocedure Async_Buffer_Status(var Insize,Infree,OutSize,Outfree: word);πbegin;π async_get_fossil_data;π insize:=fossildata.ibufr;π infree:=fossildata.ifree;π outsize:=fossildata.obufr;π outfree:=fossildata.ofree;πend;ππend.π